using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Design;
using Camalon.Drawing;

namespace Camalon.Drawing.Design
{
	public class ShapeFillEditor:UITypeEditor
	{

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.None;
        }

		public override bool GetPaintValueSupported(ITypeDescriptorContext context) 
		{
			return true;
		}

		public override void PaintValue(PaintValueEventArgs pe) 
		{
			if(pe.Value == null || !( pe.Value is ShapeFill)) 
			{
				base.PaintValue( pe);
				return;
			}

			try
			{
				ShapeFill shapeFill = pe.Value as ShapeFill;
				if( shapeFill == null) return;

				Graphics g = pe.Graphics;
				Rectangle b = pe.Bounds;

				GraphicsPath path = new GraphicsPath();
				path.AddRectangle( b );
				Brush brush = null;

				switch( shapeFill.FillType)
				{
					case FillType.Hatched:
					{
						brush = ((HatchFill)shapeFill).GetBrush();
						break;
					}
					case FillType.LinearGradient:
					{
						brush =((LinearGradientFill) shapeFill).GetBrush( b );
						break;
					}
					case FillType.PathGradient:
					{
						brush = ((PathGradientFill)shapeFill).GetBrush( path);
						break;
					}
					case FillType.Solid:
					{
						brush =((SolidFill) shapeFill).GetBrush();
						break;
					}
					case FillType.Texture:
					{
						brush = ((TextureFill)shapeFill).GetBrush();
						break;
					}
				}

				g.FillPath( brush, path );
				path.Dispose();
				brush.Dispose();

			}
			catch( Exception e )
			{
				FileWatcher.WriteLine("ShapeFillEditor::PaintValue failed:"+ e.Message +"\n"+e.StackTrace);
				throw e;
			}

		}

	
	}
}
